import math
import numpy as np

class EvaluatorWithStandard:
    def __init__(self) -> None:
        self.a = 0.00002
        self.b = 3
        self.c = 0.003
        self.standard = [[5596.40151139245, 0], [3707.0672061598975, -29], [6002.790499102289, -120], [3002.6266624020554, -76], [2677.1527929903414, -97], [2808.9989947877193, -121], [3630.6097429041397, -234], [3050.085492655377, -264], [-17232.973281441, -58], [-10822.994585736624, -89], [-5409.341358895912, -56], [-7029.887764638689, -144], [-6136.557092821199, -172], [-7664.189416937607, -206], [-5113.107238674831, -149], [-4048.658077810315, -169]]

    def evaluate(self, res):
        sum1 = 0
        sum2 = 0
        for i in range(len(self.standard)):
            sum1 += self.a * (res[i][0] - self.standard[i][0]) ** 2
            sum2 += self.b * (res[i][1] - self.standard[i][1]) ** 2
        sum = np.sqrt(sum1) + np.sqrt(sum2)
        print("sum1 = ", sum1, " sum2 = ", sum2, " sum: ", sum, "socre: ", self.score(sum))
        return self.score(sum)

    def score(self, sum):
        return 200 * (1 - 1 / ( 1 + np.exp(-self.c * sum)))

# 用于计算一组数据的一致性
# 不支持流的
class Evaluator:
    def evaluate(self, persons):  # persons personNum * frameNum * 8 维
        personNum = len(persons)
        frameNum = 1e6
        for i in range(personNum):
            frameNum = min(frameNum, len(persons[i]))
        consistency = [0 for i in range(frameNum)]
        for f in range(frameNum):  # 帧
            for p in range(1, personNum):  # 人
                for c in range(8):  # 通道
                    consistency[f] += abs(persons[p][f][c] - persons[0][f][c])
        return consistency

# 基于最大连通图的一致性计算方法
# 支持流
class GraphEvaluator:
    def evaluate(self, persons):  # persons personNum *  8 维
        persons = np.array(persons)
        res = 0
        for i in range(8):
            res += self.helpEva(persons[:,i])

        # 归一
        res = math.exp(-res / 1000)
        return res

    # 计算每一通道下的均值
    def helpEva(self, channel):
        sorted(channel)
        #全局最大值区间和局部最大值
        lMax, rMax, l, r = 0, 0, 0, 0
        for i in range(1, len(channel) - 1):
            if abs(channel[i] - channel[i - 1]) <= abs(channel[i] - channel[i - 1]):
                r = i
            else:
                if r - l > rMax - lMax:
                    rMax, lMax = r, l
                l, r = i, i
        if r - l > rMax - lMax:
            rMax, lMax = r, l
        # 最大区间的均值
        avg = np.mean(channel[lMax:rMax + 1])
        res = 0
        for i in range(len(channel)):
            res += abs(channel[i] - avg)
        
        return res